import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
import login from '@/views/login/loginPage.vue'
import Layout from '@/views/layout/LayoutContainer.vue'
import Channel from '@/views/article/ArticleChannel.vue'
import Manage from '@/views/article/ArticleManage.vue'
import Profile from '@/views/user/UserProfile.vue'
import Avatar from '@/views/user/UserAvatar.vue'
import Password from '@/views/user/UserPassword.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: login
    },
    {
      path: '/',
      component: Layout,
      redirect: '/article/channel',
      children: [
        {
          path: '/article/manage',
          component: Manage
        },
        {
          path: '/article/channel',
          component: Channel
        },
        {
          path: '/user/profile',
          component: Profile
        },
        {
          path: '/user/avatar',
          component: Avatar
        },
        {
          path: '/user/password',
          component: Password
        }
      ]
    }
  ]
})
router.beforeEach((to) => {
  const userStore = useUserStore()
  if (!userStore.token && to.path !== '/login') return '/login'
})
export default router
